package com.xysd.express.activity.logistics;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.xysd.express.R;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.bean.CompanyListObj;
import com.xysd.express.eventbus.LocationEvent;
import com.xysd.express.eventbus.LogisticsReceiveInfoEvent;
import com.xysd.express.eventbus.LogisticsSendInfoEvent;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.LoadingDialogUtils;
import com.xysd.express.util.TextUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@ContentView(R.layout.activity_edit_logistics_info)
public class EditLogisticsInfoActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.adds_send_tv)
    private TextView adds_send_tv;//发货地址
    @ViewInject(R.id.name_tel_send_tv)
    private TextView name_tel_send_tv;//发货姓名和电话
    @ViewInject(R.id.adds_receive_tv)
    private TextView adds_receive_tv;//收货地址
    @ViewInject(R.id.name_tel_receive_tv)
    private TextView name_tel_receive_tv;//收货姓名和电话
    @ViewInject(R.id.cargo_name_et)
    private EditText cargo_name_et;//货物名称
    @ViewInject(R.id.weight_et)
    private EditText weight_et;//重量
    @ViewInject(R.id.worth_et)
    private EditText worth_et;//价值
    @ViewInject(R.id.company_tv)
    private TextView company_tv;//物流公司
    @ViewInject(R.id.use_time_tv)
    private TextView use_time_tv;//用车时间
    @ViewInject(R.id.change_time_tv)
    private TextView change_time_tv;//改变时间
    @ViewInject(R.id.remakr_et)
    private EditText remakr_et;//备注
    @ViewInject(R.id.money_tv)
    private TextView money_tv;//金额
    @ViewInject(R.id.comfirm_tv)
    private TextView comfirm_tv;//提交

    private String adds_send,adds_details_send,name_send,tel_send,adds_receive,adds_detals_receive,name_receive,tel_receive;
    private String cargo_name,weight,worth,company,remark,money,receiveTime;
    private String companyId="0",carTypeId,mileage;
    private String time;
    private String ShipperLat,ShipperLng,ReceiverLat,ReceiverLng,JourneyDistance;//收发件经纬度,距离

    LogisticsSendInfoEvent sendInfoEvent;//发件信息
    LogisticsReceiveInfoEvent receiveInfoEvent;//收件信息
    LocationEvent location_send,location_receive;//收发件位置信息
    Intent intent;
    @Override
    protected void init() {
        setTitleBar("完善订单信息");
        company_tv.setOnClickListener(this);
        change_time_tv.setOnClickListener(this);
        comfirm_tv.setOnClickListener(this);
        time=getIntent().getStringExtra("time");
        if (time.equals("now")){
            change_time_tv.setVisibility(View.INVISIBLE);
        }

        intent=getIntent();
        sendInfoEvent= (LogisticsSendInfoEvent) intent.getSerializableExtra("send");
        receiveInfoEvent= (LogisticsReceiveInfoEvent) intent.getSerializableExtra("receive");

        adds_send=sendInfoEvent.adds;
        adds_details_send=sendInfoEvent.adds_details;
        name_send=sendInfoEvent.name;
        tel_send=sendInfoEvent.tel;

        adds_receive=receiveInfoEvent.adds;
        adds_detals_receive=receiveInfoEvent.adds_details;
        name_receive=receiveInfoEvent.name;
        tel_receive=receiveInfoEvent.tel;

        adds_send_tv.setText(adds_send);
        name_tel_send_tv.setText(name_send+"  "+tel_send);
        adds_receive_tv.setText(adds_receive);
        name_tel_receive_tv.setText(name_receive+"  "+tel_receive);

        //接收传过来的经纬度信息
        location_send= (LocationEvent) intent.getSerializableExtra("location_send");
        location_receive= (LocationEvent) intent.getSerializableExtra("location_receive");
        ShipperLat=location_send.model.getLating();
        ShipperLng=location_send.model.getLning();
        ReceiverLat=location_receive.model.getLating();
        ReceiverLng=location_receive.model.getLning();

        mileage=intent.getStringExtra("mileage");
        carTypeId=intent.getStringExtra("carType");
        money=intent.getStringExtra("money");
        new TextUtil().setTextColor(money_tv,money,1,money.length(),"#fa0d09");
        money=money.substring(2,money.length());//作为参数传给接口时需去掉前面的“约￥”

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCompanySelectEvent(CompanyListObj.Company event){
        companyId=event.CommpanyId+"";
        company_tv.setText(event.CompanyName);
        company=event.CompanyName;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.company_tv:
                Intent intent=new Intent(mActivity,SelectCompanyActivity.class);
                intent.putExtra("from",111);
                startActivity(intent);
                break;
            case R.id.change_time_tv:
//                ToastUtil.show("暂未支持");
                showTimePicker(use_time_tv);

                break;
            case R.id.comfirm_tv:
                cargo_name=cargo_name_et.getText().toString();
                weight=weight_et.getText().toString();
                worth=worth_et.getText().toString();
                remark=remakr_et.getText().toString();
                receiveTime=use_time_tv.getText().toString();
                if (receiveTime.equals("现在")|| TextUtils.isEmpty(receiveTime)){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    receiveTime=sdf.format(System.currentTimeMillis());
                }
                publish();
                break;
        }
    }

    //时间选择器
    private void showTimePicker(final TextView timeView) {
        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String timeStr = sdf.format(date);
                timeView.setText(timeStr);
            }
        })
                .setTitleText("选择时间")//标题文字
                .isCyclic(false)//是否循环滚动
                .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setLabel("年", "月", "日", "时", "分", "秒")
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    private void publish() {
        LoadingDialogUtils.createLoadingDialog(mActivity).show();
        HttpUtil.publishLogistics(companyId, carTypeId, cargo_name, weight, worth,
                adds_send, adds_details_send, name_send, tel_send,
                adds_receive, adds_detals_receive, name_receive,
                tel_receive, receiveTime, remark,
                ShipperLat,ShipperLng,ReceiverLat,ReceiverLng, new HttpCallBack() {
                    @Override
                    public void onSuccess(String result) {
                        LoadingDialogUtils.closeLoadingDialog();
                        Log.i("发布物流订单",result);
                        String code = JSONParser.getStringFromJsonString("Status",result);
                        String msg = JSONParser.getStringFromJsonString("Msg",result);
                        if ("0".equals(code)){
                            startAty(PayLogisticsOrderActivity.class);
                        }
                    }

                    @Override
                    public void onFailure(String error) {
                        LoadingDialogUtils.closeLoadingDialog();
                        Log.e("发布物流订单",error);
                    }
                });
    }
}
